package com.example.listviewdemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;

import android.util.Log;
import android.view.View;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private String [] mStringdata = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry",
            "Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry",
            "Mango"};


    private static final String LOG_TAG = "MainActivity";

    private ListView mListView = null;

    private Button mButtonAdd = null;
    private Button mButtonDel = null;


    private ArrayAdapter<String> mAdapter = null;
    private List mList = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initData();
    }

    private void initData() {

        mList = new ArrayList<>(Arrays.asList(mStringdata));

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
        mListView.setAdapter(mAdapter);
    }

    private void initView() {
        mListView = findViewById(R.id.listview_test);

        mButtonAdd = findViewById(R.id.button_left);
        mButtonDel = findViewById(R.id.button_right);

        mButtonAdd.setOnClickListener(new ButtonClickListener());
        mButtonDel.setOnClickListener(new ButtonClickListener());

        mListView.setOnItemLongClickListener(new ItemLongClickListener());
        mListView.setOnItemClickListener(new ItemClickListener());
    }

    private void addItem() {
        if (mListView == null && mAdapter == null) {
            return;
        }
        //showDialog();

        mAdapter.add("新元素");
    }

    private void delItem() {
        if (mListView == null && mAdapter == null) {
            return;
        }

        mAdapter.clear();
    }


    private class ItemLongClickListener implements AdapterView.OnItemLongClickListener {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            DEBUG("onItemLongClick");
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setTitle("Make sure delete current Item？");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    String itemStr = mAdapter.getItem(position);
                    mAdapter.remove(itemStr);
                }
            });
            builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            return false;
        }
    }

    private class ItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            DEBUG("onItemClick");
        }
    }

    private class ButtonClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button_left:
                    addItem();
                    break;
                case R.id.button_right:
                    delItem();
                    break;
                    default:
                        break;
            }
        }
    }


    private void DEBUG(String info) {
        Log.d(LOG_TAG, info);
    }
}
